local filename = "downvote.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"
require "pubenum"

local service = nil
local CMD = {
	service = nil
}

--
--处理拍砖行为
--
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end

    if not msgbody or not msgbody.downvoted_rid then
        return {retcode = EErrCode.ERR_INVALID_REQUEST,retstr = "无效的参数"}
    end
	
    --更新被踩玩家的被踩数
    local downvoted_rid = tonumber(msgbody.downvoted_rid)
    if downvoted_rid < 100000 or downvoted_rid > 999999 then
        return { }
    end

    local agent = skynet.call(".mainnode", "lua", "cmd", "get_agent_by_rid", downvoted_rid)
    if agent then
        skynet.send(agent, "lua", "cmd", "inc_downvoted_num",service.info.rolename)
    else 
        -- 去数据节点查询，然后修改
        local downvoted_num = 0
		local result = cluster.call("datanode", ".router", "select_rk", downvoted_rid, "rs_otherinfo",true)
		if result[1] ~= 0 then
			downvoted_num = result[2][downvoted_rid].downvoted_num or 0
            downvoted_num = downvoted_num + 1
            cluster.send("datanode", ".router", "update_rk", downvoted_rid, "rs_otherinfo", nil, {downvoted_num = downvoted_num})
		end
    end

	return {}
end

return CMD